using AForge.Video;
using AForge.Video.DirectShow;
using Declarations;
using Declarations.Filters;
using Declarations.Media;
using Declarations.Players;
using Implementation;
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace SilverSea.Video.Tools
{
	public class VLC_VideoCaptureDevice : IVideoSource
	{
		private IMediaPlayerFactory imediaPlayerFactory_0;
		private IVideoPlayer ivideoPlayer_0;
		private IMedia imedia_0;
		private IMemoryRenderer imemoryRenderer_0;
		private IAdjustFilter iadjustFilter_0;
		private string string_0;
		private string string_1;
		private string string_2;
		private int int_0;
		private object object_0;
		private int int_1;
		private int int_2;
		private bool bool_0 = true;
		private int int_3 = 10000;
		private System.Drawing.Size size_0 = new System.Drawing.Size(0, 0);
		private int int_4;
		private bool bool_1 = true;
		private string string_3 = "Deinterlace Filter";
		private object object_1 = new object();
        private Declarations.NewFrameEventHandler newFrameEventHandler_0;
		private AForge.Video.NewFrameEventHandler newFrameEventHandler_1;
		private VideoSourceErrorEventHandler videoSourceErrorEventHandler_0;
		private PlayingFinishedEventHandler playingFinishedEventHandler_0;
        public event AForge.Video.NewFrameEventHandler NewFrame;
        //{
        //    add
        //    {
        //        AForge.Video.NewFrameEventHandler newFrameEventHandler = this.newFrameEventHandler_1;
        //        AForge.Video.NewFrameEventHandler newFrameEventHandler2;
        //        do
        //        {
        //            newFrameEventHandler2 = newFrameEventHandler;
        //            NewFrameEventHandler value2 = (NewFrameEventHandler)Delegate.Combine(newFrameEventHandler2, value);
        //            newFrameEventHandler = Interlocked.CompareExchange<NewFrameEventHandler>(ref this.newFrameEventHandler_1, value2, newFrameEventHandler2);
        //        }
        //        while (newFrameEventHandler != newFrameEventHandler2);
        //    }
        //    remove
        //    {
        //        NewFrameEventHandler newFrameEventHandler = this.newFrameEventHandler_1;
        //        NewFrameEventHandler newFrameEventHandler2;
        //        do
        //        {
        //            newFrameEventHandler2 = newFrameEventHandler;
        //            NewFrameEventHandler value2 = (NewFrameEventHandler)Delegate.Remove(newFrameEventHandler2, value);
        //            newFrameEventHandler = Interlocked.CompareExchange<NewFrameEventHandler>(ref this.newFrameEventHandler_1, value2, newFrameEventHandler2);
        //        }
        //        while (newFrameEventHandler != newFrameEventHandler2);
        //    }
        //}
        public event VideoSourceErrorEventHandler VideoSourceError;
        //{
        //    add
        //    {
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
        //        do
        //        {
        //            videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
        //            VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Combine(videoSourceErrorEventHandler2, value);
        //            videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
        //        }
        //        while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
        //    }
        //    remove
        //    {
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
        //        VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
        //        do
        //        {
        //            videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
        //            VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Remove(videoSourceErrorEventHandler2, value);
        //            videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
        //        }
        //        while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
        //    }
        //}
        public event PlayingFinishedEventHandler PlayingFinished;
        //{
        //    add
        //    {
        //        PlayingFinishedEventHandler playingFinishedEventHandler = this.playingFinishedEventHandler_0;
        //        PlayingFinishedEventHandler playingFinishedEventHandler2;
        //        do
        //        {
        //            playingFinishedEventHandler2 = playingFinishedEventHandler;
        //            PlayingFinishedEventHandler value2 = (PlayingFinishedEventHandler)Delegate.Combine(playingFinishedEventHandler2, value);
        //            playingFinishedEventHandler = Interlocked.CompareExchange<PlayingFinishedEventHandler>(ref this.playingFinishedEventHandler_0, value2, playingFinishedEventHandler2);
        //        }
        //        while (playingFinishedEventHandler != playingFinishedEventHandler2);
        //    }
        //    remove
        //    {
        //        PlayingFinishedEventHandler playingFinishedEventHandler = this.playingFinishedEventHandler_0;
        //        PlayingFinishedEventHandler playingFinishedEventHandler2;
        //        do
        //        {
        //            playingFinishedEventHandler2 = playingFinishedEventHandler;
        //            PlayingFinishedEventHandler value2 = (PlayingFinishedEventHandler)Delegate.Remove(playingFinishedEventHandler2, value);
        //            playingFinishedEventHandler = Interlocked.CompareExchange<PlayingFinishedEventHandler>(ref this.playingFinishedEventHandler_0, value2, playingFinishedEventHandler2);
        //        }
        //        while (playingFinishedEventHandler != playingFinishedEventHandler2);
        //    }
        //}
		public bool SeparateConnectionGroup
		{
			get
			{
				return this.bool_0;
			}
			set
			{
				this.bool_0 = value;
			}
		}
		public string Source
		{
			get
			{
				return this.string_0;
			}
			set
			{
				this.string_0 = value;
			}
		}
		public string Login
		{
			get
			{
				return this.string_1;
			}
			set
			{
				this.string_1 = value;
			}
		}
		public string Password
		{
			get
			{
				return this.string_2;
			}
			set
			{
				this.string_2 = value;
			}
		}
		public int Chanel
		{
			get
			{
				return this.int_0;
			}
			set
			{
				this.int_0 = value;
			}
		}
		public int FramesReceived
		{
			get
			{
				int result = this.int_1;
				this.int_1 = 0;
				return result;
			}
			set
			{
				this.int_1 = value;
			}
		}
		public int BytesReceived
		{
			get
			{
				int result = this.int_2;
				this.int_2 = 0;
				return result;
			}
			set
			{
				this.int_2 = value;
			}
		}
		public object UserData
		{
			get
			{
				return this.object_0;
			}
			set
			{
				this.object_0 = value;
			}
		}
		public int RequestTimeout
		{
			get
			{
				return this.int_3;
			}
			set
			{
				this.int_3 = value;
			}
		}
		public bool UsingDeInterlace
		{
			get
			{
				return this.bool_1;
			}
			set
			{
				this.bool_1 = value;
			}
		}
		public string DeinterlaceFilter
		{
			get
			{
				return this.string_3;
			}
			set
			{
				this.string_3 = value;
			}
		}
		public System.Drawing.Size DesiredFrameSize
		{
			get
			{
				return this.size_0;
			}
			set
			{
				this.size_0 = value;
			}
		}
		public int DesiredFrameRate
		{
			get
			{
				return this.int_4;
			}
			set
			{
				this.int_4 = value;
			}
		}
		public bool IsRunning
		{
			get
			{
				bool result = false;
				lock (this.object_1)
				{
					if (this.ivideoPlayer_0 != null)
					{
                        result = this.ivideoPlayer_0.IsPlaying;
					}
				}
				return result;
			}
		}
		public bool EnableAdjustFilter
		{
			set
			{
				lock (this.object_1)
				{
					if (this.ivideoPlayer_0 != null && value)
					{
                        this.iadjustFilter_0 = this.ivideoPlayer_0.Adjust;
					}
					else
					{
						this.iadjustFilter_0 = null;
					}
				}
			}
		}
		public float Brightness
		{
			get
			{
				if (this.iadjustFilter_0 != null)
				{
					return this.iadjustFilter_0.Brightness;
				}
				return 0f;
			}
			set
			{
				lock (this.object_1)
				{
					if (this.iadjustFilter_0 != null)
					{
                        this.iadjustFilter_0.Enabled = true;
						this.iadjustFilter_0.Brightness = value;
					}
				}
			}
		}
		public float Contrast
		{
			get
			{
				float result = 0f;
				lock (this.object_1)
				{
					if (this.iadjustFilter_0 != null)
					{
                        result = this.iadjustFilter_0.Contrast;
					}
				}
				return result;
			}
			set
			{
				lock (this.object_1)
				{
					if (this.iadjustFilter_0 != null)
					{
						this.iadjustFilter_0.Enabled = true;
						this.iadjustFilter_0.Contrast = value;
					}
				}
			}
		}
		public float Gamma
		{
			get
			{
				float result = 0f;
				lock (this.object_1)
				{
					if (this.iadjustFilter_0 != null)
					{
                        result = this.iadjustFilter_0.Gamma;
					}
				}
				return result;
			}
			set
			{
				lock (this.object_1)
				{
					if (this.iadjustFilter_0 != null)
					{
                        this.iadjustFilter_0.Enabled = true;
                        this.iadjustFilter_0.Gamma = value;
					}
				}
			}
		}
		public void Start()
		{
            try
            {
                string str = string.Empty;
                FilterInfoCollection infos = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                int num = 0;
                while (num < infos.Count)
                {
                    if (infos[num].MonikerString == this.string_0)
                    {
                        goto Label_003D;
                    }
                    num++;
                }
                goto Label_004A;
            Label_003D:
                str = infos[num].Name;
            Label_004A:
                if (this.int_0 > 0)
                {
                    str = str + " #" + this.int_0;
                }
                this.imediaPlayerFactory_0 = new MediaPlayerFactory();
                this.ivideoPlayer_0 = this.imediaPlayerFactory_0.CreatePlayer<IVideoPlayer>();
                int width = 640;
                int height = 480;
                if ((this.size_0.Width > 0) && (this.size_0.Height > 0))
                {
                    width = this.size_0.Width;
                    height = this.size_0.Height;
                }
                string str2 = width + "x" + height;
                int num4 = 0x19;
                if ((this.int_4 > 0) && (this.int_4 <= 0x19))
                {
                    num4 = this.int_4;
                }
                this.imedia_0 = this.imediaPlayerFactory_0.CreateMedia<IMedia>("dshow://", new string[] {
                    "dshow-vdev=" + str,
                    "dshow-adev=none",
                    "dshow-size=" + str2,
                    "dshow-fps=" + num4,
                    "dshow-caching=10"
                });
                this.imemoryRenderer_0 = this.ivideoPlayer_0.CustomRenderer;
                //this.imemoryRenderer_0.SetFormat(new BitmapFormat(width, height, 2));
                this.imemoryRenderer_0.SetFormat(new BitmapFormat(width, height,  ChromaType.RV24));
                this.newFrameEventHandler_0 = new Declarations.NewFrameEventHandler(this.method_0);
                this.imemoryRenderer_0.SetCallback(this.newFrameEventHandler_0);
                this.imedia_0.Parse(true);
                this.ivideoPlayer_0.Open(this.imedia_0);
                this.ivideoPlayer_0.Play();
                if (this.bool_1)
                {
                    DeinterlaceMode mode;
                    if (this.string_3 == "bob")
                    {
                        mode = DeinterlaceMode.bob;
                    }
                    else
                    {
                        if (this.string_3 == "discard")
                        {
                            mode = DeinterlaceMode.discard;
                        }
                        else
                        {
                            if (this.string_3 == "linear")
                            {
                                mode = DeinterlaceMode.linear;
                            }
                            else
                            {
                                if (this.string_3 == "mean")
                                {
                                    mode = DeinterlaceMode.mean;
                                }
                                else
                                {
                                    if (this.string_3 == "x")
                                    {
                                        mode = DeinterlaceMode.x;
                                    }
                                    else
                                    {
                                        if (this.string_3 == "yadif")
                                        {
                                            mode = DeinterlaceMode.yadif;
                                        }
                                        else
                                        {
                                            if (this.string_3 == "yadif2x")
                                            {
                                                mode = DeinterlaceMode.yadif2x;
                                            }
                                            else
                                            {
                                                mode = DeinterlaceMode.blend;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    this.ivideoPlayer_0.Deinterlace.Mode = mode;
                    this.ivideoPlayer_0.Deinterlace.Enabled = true;
                }
            }
            catch (Exception exception)
            {
                if (this.videoSourceErrorEventHandler_0 != null)
                {
                    this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs(exception.Message));
                }
            }
		}
		private void method_0(System.Drawing.Bitmap bitmap_0)
		{
			this.int_1++;
			if (this.newFrameEventHandler_1 != null)
			{
				this.newFrameEventHandler_1.Invoke(this, new NewFrameEventArgs(bitmap_0));
				bitmap_0.Dispose();
				bitmap_0 = null;
			}
		}
		public void SignalToStop()
		{
			if (this.IsRunning)
			{
				this.ivideoPlayer_0.Stop();
				Application.DoEvents();
			}
		}
		public void WaitForStop()
		{
		}
		public void Stop()
		{
			try
			{
				lock (this.object_1)
				{
					if (this.imemoryRenderer_0 != null)
					{
						this.imemoryRenderer_0 = null;
					}
					Application.DoEvents();
					if (this.imedia_0 != null)
					{
						this.imedia_0.Dispose();
						this.imedia_0 = null;
					}
					Application.DoEvents();
					if (this.ivideoPlayer_0 != null)
					{
						this.ivideoPlayer_0.Dispose();
						this.ivideoPlayer_0 = null;
					}
					Application.DoEvents();
					if (this.imediaPlayerFactory_0 != null)
					{
						this.imediaPlayerFactory_0.Dispose();
						this.imediaPlayerFactory_0 = null;
					}
					Application.DoEvents();
					if (this.playingFinishedEventHandler_0 != null)
					{
						this.playingFinishedEventHandler_0.Invoke(this, ReasonToFinishPlaying.StoppedByUser);
					}
				}
			}
			catch (Exception ex)
			{
				if (this.videoSourceErrorEventHandler_0 != null)
				{
					this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs(ex.Message));
				}
			}
		}
	}
}
